package com.apes.scm.settle.settle.respository;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.settle.settle.model.SettleItem;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

public interface SettleItemRepository extends CustomRepository<SettleItem,String> {

    @Query(
            value = " select new map(s.supplierRef as supplierRef,s.id as sid,s.invoiceItem.invoice.id as invoiceItemId, s.origin as origin,s.settle.approveDate as approveDate, s.originBusinessType as originBusinessType, s.originItemId as originItemId, s.priceUnit as priceUnit," +
                    " s.invoiceQty as invoiceQty, s.pendingQty as pendingQty, s.actualQty as actualQty, s.amtUntaxed as amtUntaxed, s.amtTax as amtTax, s.amount as amount, s.invoiceItem.id as invoiceItemId,  " +
                    " p.id as productId , p.name as productName, u.id as uomId, u.name as uomName, t.id as taxId, t.name as taxName, s.discount as discount," +
                    "s.occurrenceDate as occurrenceDate,s.carLicense as carLicense,z.dept.id as deptId,z.dept.name as deptName,s.note as note," +
                    " s.invoiceItem.invoice.source as source, s.invoiceItem.invoice.sourceBusinessType as sourceBusinessType,s.floorPrice as floorPrice)" +
                    " from SettleItem s left join s.product p left join s.settle z left join s.uom u left join s.tax t " +
                    " where s.settle.id = ?1"
    )
    List<Map<String,Object>> findAllBySettleId(String id);

    /*
    * 批量删除
    * */
    @Modifying
    @Query(value = "delete from SettleItem where settle.id = ?1")
    void deleteAllBySettleId(String settleId);

    @Query(
            value = " select new map(a.amount as payAmount,c.amtTotal as  totalAmount)" +
                    " from PaymentRegisterItem a, TaxBillItem b ,TaxBill c" +
                    " where a.taxBillItem.id = b.id and b.taxBill.id = c.id and c.settle.id = ?1"
    )
    List<Map<String,Object>>findAmountBySettleId(String id);

}
